<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <!-- 引入axios -->
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
    <!-- 引入Vue -->
    <script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
    <!-- import Element UI -->
    <script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
    <link rel="stylesheet" href="css/AddAdmin.css">
</head>
<body>
<div id="app">
    <el-container>
        <el-header>
            <h1>
                <img src="../imgs/shark.png" id="logo">
                CoolShark商城管理系统
                <span v-if="" style="float: right;font-size: 15px;">
                    <a href="JavaScript:void(0)" style="text-decoration: none ;font-family: 楷体">退出登录</a>
                </span>
            </h1>
        </el-header>
        <el-container>
            <el-aside>
                <!-- :unique-opened="true":设置子菜单只允许打开一个--->
                <el-menu
                        default-active="4-1"
                        class="el-menu-vertical-demo"
                        :unique-opened="true"
                        @select="handleSelect"
                        @open="handleOpen"
                        @close="handleClose">
                    <el-menu-item index="1">
                        <i class="el-icon-s-home"></i>
                        <span slot="title">首页</span>
                    </el-menu-item>
                    <el-submenu index="2">
                        <template slot="title">
                            <i class="el-icon-user-solid"></i>
                            <span>管理员管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="2-1" v-if="username==='root'">添加管理员</el-menu-item>
                            <el-menu-item index="2-2">管理员列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-submenu index="3">
                        <template slot="title">
                            <i class="el-icon-s-claim"></i>
                            <span>类别管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="3-1">添加类别</el-menu-item>
                            <el-menu-item index="3-2">类别列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-submenu index="4">
                        <template slot="title">
                            <i class="el-icon-s-order"></i>
                            <span>商品管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="4-1">添加商品</el-menu-item>
                            <el-menu-item index="4-2">商品列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-submenu index="5">
                        <template slot="title">
                            <i class="el-icon-picture"></i>
                            <span>轮播图管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="5-1">添加轮播图</el-menu-item>
                            <el-menu-item index="5-2">轮播图列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-menu-item index="6">
                        <i class = "el-icon-user-solid"></i>
                        <span>个人信息</span>
                    </el-menu-item>
                </el-menu>
            </el-aside>
            <el-main>
                <el-card style="height: 600px">
                    <el-form :model="goods" :rules="rules" ref="adminForm" label-width="80px">
                        <el-form-item label="商品名" prop="title">
                            <el-input v-model="goods.title" placeholder="请输入用户名"></el-input>
                        </el-form-item>
                        <el-form-item label="商品分类">
                            <el-select placeholder="请选择" v-model="goods.carouselId">
                                <el-option v-for="c in carousel" :label="c.name" :value="c.id"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="商品价格" prop="price">
                            <el-input v-model="goods.price" placeholder="请输入商品价格"></el-input>
                        </el-form-item>
                        <el-form-item label="商品库存" prop="stock">
                            <el-input v-model="goods.stock" placeholder="请输入商品库存"></el-input>
                        </el-form-item>
                        <el-form-item label="商品图片">
                            <el-upload
                                    action="/goods"
                                    name="picFile"
                                    limit="1"
                                    list-type="picture-card"
                                    :on-success="handleSuccess"
                                    :on-preview="handlePictureCardPreview"
                                    :on-remove="handleRemove">
                                <i class="el-icon-plus"></i>
                            </el-upload>
                            <el-dialog :visible.sync="dialogVisible">
                                <img width="100%" :src="dialogImageUrl" alt="">
                            </el-dialog>
                        </el-form-item>
                        <el-form-item label="商品状态" prop="enable">
                            <el-select placeholder="请选择" v-model="goods.enable">
                                <el-option label="上架" value="1"></el-option>
                                <el-option label="下架" value="0"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="insert">添加</el-button>
                            <el-button type="primary">重置</el-button>
                        </el-form-item>
                    </el-form>
                </el-card>
            </el-main>
        </el-container>
    </el-container>
</div>
</body>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                username: localStorage.getItem('username'),
                carousel: [],
                dialogImageUrl: '',
                dialogVisible: false,
                goods: {
                    title: '',
                    carouselId: '',
                    price: '',
                    stock: '',
                    img: '',
                    enable: ''
                },
                rules: {
                    title: [
                        {required: true, message: '请输入商品名', trigger: 'blur'},
                    ],
                    price: [
                        {required: true, message: '请输入商品价格', trigger: 'blur'},
                    ],
                    stock: [
                        {required: true, message: '请输入商品库存', trigger: 'blur'},
                    ],
                    keyword: [
                        {required: true, message: '请输入关键字', trigger: 'blur'},
                    ],
                    img: [
                        {required: true, message: '请输入商品图片', trigger: 'blur'},
                    ],
                    enable: [
                        {required: true, message: '请选择商品状态', trigger: 'blur'},
                    ],
                }
            }
        },
        methods: {
            /**
             * 根据不同的key值，重定向到相应的页面。
             * key: 用于识别要跳转到哪个页面的标识符。
             * 无返回值。
             */
            handleSelect(key, keyPath) {
                console.log(v.username);
                if (key ==='1') {
                    location.href='index.html'
                }else if (key === '2-1') {
                    location.href = 'AddAdmin.html'; // 跳转到添加管理员页面
                } else if (key === '2-2') {
                    location.href = 'AdminList.html'; // 跳转到管理员列表页面
                } else if (key === '3-1') {
                    let name = prompt("请输入类别名称：", "");
                    if (name === null) {
                        return;
                    }
                    if (name.trim() === '') {
                        v.$message({
                            message: '类别名称不能为空！',
                            type: 'warning'
                        });
                    }
                    axios.get('/admin/insertCarousel?name=' + name)
                        .then(function (response) {
                            v.carousel = response.data.data;
                            location.href = 'CarouselList.html'
                        });
                } else if (key === '3-2') {
                    location.href = 'CarouselList.html'; // 跳转到类别列表页面
                } else if (key === '4-2') {
                    location.href = 'GoodsList.html'
                } else if (key === '5-1') {
                    location.href = 'AddBanner.html'
                } else if (key === '5-2') {
                    location.href = 'BannerList.html'
                }else if (key === '6') {
                    location.href = 'PersonalInfo.html'
                }
            },
            handleOpen(key, keyPath) {
                console.log('key = ' + key, 'leyPath = ' + keyPath);
            },
            handleClose(key, keyPath) {
                console.log('key = ' + key, 'leyPath = ' + keyPath);
            },
            handleSuccess(response, file, fileList) {
                // response代表图名
                v.goods.img = response;
                console.log('图片链接 = ' + v.goods.img)
            },
            handleRemove(file, fileList) {
                console.log(file, fileList);
                v.goods.img = '';
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            insert() {
                console.log("商品名称 = " + v.goods.title)
                console.log("商品价格 = " + v.goods.price)
                console.log("商品库存 = " + v.goods.stock)
                console.log("类别id = " + v.goods.carouselId)
                console.log("商品图片 = " + v.goods.img)
                console.log("关键字 = " + v.goods.keyword)
                console.log("商品状态 = " + v.goods.enable)
                axios.post('/goods/insert', v.goods).then(function (response) {
                    let responseBody = response.data;
                    if (responseBody.state === 20000) {
                        v.$message({
                            message: '添加成功！',
                            type: 'success'
                        });
                        location.replace("/AddGoods.html")
                    } else {
                        v.$message.error(responseBody.message);
                    }
                })
            },
        },
        created: function () {
            axios.get('/carousel/list')
                .then(function (response) {
                    v.carousel = response.data.data;
                });
        }
    })
</script>
</html>